package com.xuerhua.yxt.controller.student;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.xuerhua.yxt.common.Const;
import com.xuerhua.yxt.common.MenuConst;
import com.xuerhua.yxt.common.RspResult;
import com.xuerhua.yxt.common.enums.RelationType;
import com.xuerhua.yxt.common.enums.Status;
import com.xuerhua.yxt.controller.BaseController;
import com.xuerhua.yxt.model.ParentInfo;
import com.xuerhua.yxt.model.StudentInfo;
import com.xuerhua.yxt.reqVo.ParentReqVO;
import com.xuerhua.yxt.reqVo.StudentReqVO;
import com.xuerhua.yxt.service.IParentService;
import com.xuerhua.yxt.service.IStudentService;
import com.xuerhua.yxt.utils.BeanUtils;
import com.xuerhua.yxt.utils.FileUtils;
import com.xuerhua.yxt.view.ParentRelStudentView;
import com.xuerhua.yxt.wechat.model.WechatUserInfo;
import com.xuerhua.yxt.wechat.utils.WechatUtils;

@Controller
public class StudentBindController extends BaseController{
	
	@Autowired
	private IStudentService studentService;
	@Autowired
	private IParentService parentService;
	
	/**
	 * 家长绑定
	 * @return
	 */
	@RequestMapping(MenuConst.STUDENT_BIND)
	public String studentBind(Model model){
		model.addAttribute("user.currUrl", Const.STUDENT_ROLE);
		return "student/bind";
	}
	
	@RequestMapping(MenuConst.STUDENT_BIND + "/submit")
	@ResponseBody Object teacherBindSubmit(StudentReqVO vo) {
		try {
			List<StudentInfo> list = studentService.selectListByParam(vo);
			if(!(list != null && list.size() > 0)) {
				return new RspResult(Status.PWD_ERROR1);
			}
			String openid = getOpenid();
			StudentInfo studentInfo = list.get(0);
			ParentReqVO reqVO = new ParentReqVO();
			reqVO.setStudentId(studentInfo.getId());
			reqVO.setOpenid(openid);
			reqVO.setSchoolId(studentInfo.getSchoolId());
			int count = parentService.selectStudentCountForParent(reqVO);
			if(count > 0) {
				return new RspResult(Status.HAD_BIND_STUDENT);
			}
			ParentInfo parentInfo = parentService.selectByOpenid(openid);
			ParentRelStudentView parentView = new ParentRelStudentView();
			if(parentInfo != null) {
				BeanUtils.copy(parentInfo, parentView);
			}else {
				WechatUserInfo userInfo = WechatUtils.getUserInfo(WechatUtils.getAccessToken(), openid);
				String headImgUrl = FileUtils.writeHttpFile(userInfo.getHeadimgurl(), RelationType.HEAD_IMG_URL.getCode());
				parentView.setName(userInfo.getNickname());
				parentView.setOpenid(userInfo.getOpenid());
				parentView.setHeadImgUrl(headImgUrl);
			}
			parentView.setStudentId(studentInfo.getId());
			parentView.setSchoolId(studentInfo.getSchoolId());
			parentService.saveParentRelStudent(parentView);
			return new RspResult(Status.SUCCESS, parentView);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return new RspResult(Status.FAIL);
	}
}
